import React from 'react';
import {Tag, Tooltip} from 'antd';
import {message, Upload} from 'antd';
import Counter from "../../mobx-store";
import {IMPORT_IMAGE_URL} from "./api";


const ImageUpload: React.FC = () => {

    const onChange = async (info: any) => {
        if (info.file.status === 'done') {
            message.success(`${info.file.name} file uploaded successfully`);
            // 将链接复制到剪贴板
            const downloadUrl = info.file.response.downloadUrl; // 假设下载链接存储在 response.downloadUrl 中
            try {
                await navigator.clipboard.writeText(downloadUrl);
                message.success('链接已复制到剪贴板');
            } catch (error) {
                message.error('复制链接失败');
            }
        }
    }

    return (
        <>
            <Upload
                multiple={false}
                name='file'
                showUploadList={false}//不展示文件列表
                action={IMPORT_IMAGE_URL}
                data={{path: Counter.path}}
                headers={{
                    authorization: 'authorization-text',
                }}
                onChange={onChange}>
                <Tooltip placement="right">
                    <Tag color="blue">
                        导入
                    </Tag>
                </Tooltip>
            </Upload>
        </>
    )
}


export default ImageUpload;
